package com.partvoyage.client.mvp2.view.carte.bean;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.maps.client.geocode.DirectionResults;
import com.partvoyage.client.outil.PointSurLaCarte;

public class RouteSetBean {
	private List<RouteBean> routeBeans;

	public void addRoute(PointSurLaCarte source, PointSurLaCarte destination,
			DirectionResults result) {
		if (isRouteExistante(source, destination)) {
			return;
		}
		if (routeBeans == null) {
			routeBeans = new ArrayList<RouteBean>();
		}
		RouteBean route = new RouteBean();
		route.setSource(source);
		route.setDestination(destination);
		route.setDirectionResult(result);
		routeBeans.add(route);
	}

	public List<RouteBean> getRouteBeans() {
		return routeBeans;
	}

	public void reset() {
		routeBeans = null;
	}

	private Boolean isRouteExistante(PointSurLaCarte source,
			PointSurLaCarte destination) {
		if (routeBeans == null) {
			return false;
		}
		Long markerIdSource = source.getInfo().getMarkerId();
		Long markerIdDestination = destination.getInfo().getMarkerId();
		for (RouteBean routeBean : routeBeans) {
			Long markerIdSourceToCompare = routeBean.getSource().getInfo()
					.getMarkerId();
			Long markerIdDestinationToCompare = routeBean.getDestination()
					.getInfo().getMarkerId();
			if (markerIdSourceToCompare.equals(markerIdSource)
					&& markerIdDestinationToCompare.equals(markerIdDestination)
					|| markerIdSourceToCompare.equals(markerIdDestination)
					&& markerIdDestinationToCompare.equals(markerIdSource)) {
				return true;
			}

		}
		return false;
	}
}
